<template>
  <views class="account_list">
    <!-- 新增/修改 -->
    <el-dialog
      :title="type == 1 ? '新增账号' : '修改账号'"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <div class="dialog_content">
        <el-form ref="form" label-width="auto">
          <el-form-item label="用户名:">
            <el-input
              type="text"
              clearable
              placeholder="请输入用户"
              v-model="form.account"
              :disabled="type == 1 ? false : true"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码:">
            <el-input
              type="password"
              clearable
              placeholder="请输入密码"
              v-model="form.password"
            ></el-input>
          </el-form-item>
          <el-form-item label="真实姓名:">
            <el-input
              type="text"
              clearable
              placeholder="请输入真实姓名"
              v-model="form.user_name"
            ></el-input>
          </el-form-item>
          <el-form-item label="所属组织:">
            <el-select
              v-model="form.department_id"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in department_list"
                :key="item.department_id"
                :label="item.department_name"
                :value="item.department_id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属角色:">
            <el-select v-model="form.role_id" clearable placeholder="请选择">
              <el-option
                v-for="item in role_list"
                :key="item.role_id"
                :label="item.role_name"
                :value="item.role_id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="电话:">
            <el-input
              type="text"
              clearable
              placeholder="请输入电话"
              v-model="form.mobile"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱:">
            <el-input
              type="text"
              clearable
              placeholder="请输入邮箱"
              v-model="form.email"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </span>
    </el-dialog>

    <viewTitle :title="'账号列表'"></viewTitle>
    <div class="search">
      <el-form ref="form" label-width="auto" :inline="true">
        <el-form-item label="用户姓名:">
          <el-input
            type="text"
            clearable
            placeholder="请输入用户姓名"
            v-model="user_name"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色选择:">
          <el-select v-model="role_id" clearable placeholder="请选择">
            <el-option
              v-for="item in role_list"
              :key="item.role_id"
              :label="item.role_name"
              :value="item.role_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组织选择:">
          <el-select v-model="department_id" clearable placeholder="请选择">
            <el-option
              v-for="item in department_list"
              :key="item.department_id"
              :label="item.department_name"
              :value="item.department_id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div class="btn-area">
        <el-button type="primary" @click="getData">查询</el-button>
        <el-button @click="reset">重置</el-button>
        <el-button type="warning" v-if="$route.query.check.i" @click="add"
          >新增</el-button
        >
      </div>
    </div>
    <div class="table_list">
      <el-table
        :data="tableList"
        style="width: 100%"
        border
        :header-cell-style="getRowClass"
      >
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="account" label="用户名"></el-table-column>
        <el-table-column prop="user_name" label="姓名"></el-table-column>
        <el-table-column prop="role_name" label="角色" width="auto"></el-table-column>
        <el-table-column
          prop="department_name"
          label="组织"
          width="auto"
        ></el-table-column>
        <el-table-column
          prop="last_login_time"
          label="最后登录时间"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="edit(scope.row)"
              v-if="$route.query.check.m"
              icon="el-icon-edit"
              >修改</el-button
            >
            <el-button
              v-if="scope.row.is_default == 0 && $route.query.check.d"
              type="danger"
              @click="deleted(scope.row)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="flex flexR paging">
        <el-pagination
          style="background:#fff; padding:10px 0"
          background
          layout="prev, pager, next,sizes,total, jumper"
          @size-change="
            page_size => {
              this.pagination.page_size = page_size;
              this.getData();
            }
          "
          @current-change="
            page_size => {
              this.pagination.page_start = page_size;
              this.getData();
            }
          "
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.page_size"
          :total="pagination.total_count"
          :current-page="pagination.current_page"
          :page-count="pagination.page_count"
        ></el-pagination>
      </div>
    </div>
  </views>
</template>

<script>
import route_list from "../../router/route_list";

export default {
  data() {
    return {
      form: {
        user_name: "admin", // 用户名
        account: "admin", // 用户名
        password: "123456", // 密码
        user_name: "周建", // 真实姓名
        mobile: "", //  电话
        email: "", //  邮箱
        role_id: 0,
        department_id: 0
      },
      user_name: "",
      role_id: "", // 角色
      role_list: [], // 角色列表
      department_id: "", // 组织
      department_list: [], // 组织列表
      // -------- 新增修改 -------- //
      add_role: "",
      add_organization: "",
      tableList: [],
      pagination: {
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      },
      type: 1, // 1 新增 2 修改
      dialogVisible: false // 弹框展示
    };
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },
    // 获取数据
    getData() {
      this.$raxios
        .post(this.$mapapi.admin_userList, { data: this.query_data })
        .then(res => {
          if (res.code == 200) {
            this.tableList = res.data.list || [];
            this.pagination = res.data.pagination || [];
          } else {
            this.tableList = [];
            this.pagination = [];
          }
        });
    },
    // 去详情
    toDetail() {
      this.$toRoutePath({
        path: "/member_list/member_list_detail",
        edit: true,
        query: { type: 1 }
      });
    },
    // 重置
    reset() {
      this.user_name = "";
      this.role_id = "";
      this.department_id = "";
      this.getData();
    },
    // 新增
    add() {
      this.form = {};
      this.type = 1;
      this.dialogVisible = true;
    },
    // 修改
    edit(row) {
      this.form = row;
      this.type = 2;
      this.dialogVisible = true;
    },
    // 删除
    deleted(row) {
      this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let post_data = {
            user_id: row.user_id
          };
          this.$raxios
            .post(this.$mapapi.admin_delUser, { data: post_data })
            .then(res => {
              if (res.code == 200) {
                this.tableList = this.tableList.filter(item => {
                  return item.user_id != row.user_id;
                });
                this.$message({ type: "success", message: "删除成功!" });
              } else {
                this.regionList = [];
                return;
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 关闭
    handleClose() {
      this.dialogVisible = false;
    },
    saveUser() {
      if (!this.$validator().isMobile(this.save_data.mobile)) {
        this.$message.error("请输入正确的手机号码");
        return;
      }
      this.$raxios
        .post(this.$mapapi.admin_saveUser, { data: this.save_data })
        .then(res => {
          if (res.code == 200) {
            this.dialogVisible = false;
            this.$message({ type: "success", message: "保存成功!" });
            this.getData();
          } else {
            this.regionList = [];
            return;
          }
        });
    }
  },
  computed: {
    query_data() {
      let data = {};
      data.user_name = this.user_name;
      data.role_id = this.role_id;
      data.department_id = this.department_id;
      data.pagination = this.pagination;
      return data;
    },
    save_data() {
      let data = this.form;
      return data;
    }
  },
  mounted() {
    this.getData();
  },
  created() {
    this.$raxios.post(this.$mapapi.admin_allRole, {}).then(res => {
      if (res.code == 200) {
        this.role_list = res.data || [];
      }else{
	      this.$message.error(res.msg)
      }
    });

    this.$raxios.post(this.$mapapi.admin_allDepartment, {}).then(res => {
      if (res.code == 200) {
        this.department_list = res.data || [];
      }else{
	      this.$message.error(res.msg)
      }
    });
  }
};
</script>

<style
  lang="less"
  :scope="true"
  src="@/style/system_manager/account_list.less"
></style>
